/* 
 *   Date       Developer   Description
 *   
 *
 *   Classname:  com.ytincl.scdp.scdk.db.utils.ClobTest
 * 
 *   Date:   2010-6-21
 * 
 *   The source code contained in this listing is proprietary to
 *   HLJ POST YITONG INFO-NET CO.,LTD.
 * 
 *   Unauthorized copying, adaptation, distribution, use, or display
 *   is strictly prohibited.
 *   This software is Copyright 2010 HLJ POST YITONG INFO-NET CO.,LTD.
 */
package demo.database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import com.titan.core.utils.db.DBUtils;


public class ClobTest {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        Statement stmt = null;
        ResultSet rest = null;
        try {
            conn = DBUtils.getOracleConnection("10.54.121.252", "1521", "GSKFDB", "train", "train", true);
            
            try {
                DBUtils.execute(conn, "CREATE TABLE clob_test (clob_col CLOB)");
            } catch (Exception e) {
            }

            // 写操作
            String sql = "INSERT INTO clob_test VALUES(?)";
            pstmt = conn.prepareStatement(sql);
            StringBuffer bigString = new StringBuffer(1024*10);
            for (int i = 0; i < 1024; i++) {
                //bigString.append("1234567890");
                bigString.append("中国人民中国人民中国");
            }
            DBUtils.setClobAsString(pstmt, 1, bigString.toString());
            pstmt.executeUpdate();
            DBUtils.close(pstmt);

            // 修改操作
            pstmt = conn.prepareStatement("update clob_test set clob_col=?");
            String value = bigString.toString().replaceFirst("中国人民", "华夏子孙");
            DBUtils.setClobAsString(pstmt, 1, value);
            pstmt.executeUpdate();
            
            // 读操作
            stmt = conn.createStatement();
            rest = stmt.executeQuery("select clob_col from clob_test");
            while(rest.next()) {
                String clob = DBUtils.getClobAsString(rest, "clob_col");
                System.out.println(clob);
                System.out.println(clob.length());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                DBUtils.execute(conn, "DROP TABLE clob_test");
            } catch (Exception e) {
            }            
            DBUtils.close(rest);
            DBUtils.close(stmt);
            DBUtils.close(pstmt);
            DBUtils.close(conn);
        }    
    }
}

